// pages/paymentCode/balance/index.js
const app = getApp()
var call = require("../../../utils/request.js");
const { barcode, qrcode } = require('../../../utils/index.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageStatus: false,
    refreshStatus: true, // 刷新状态
    currentData : 0,
    payCode:'',//付款码
    userInfo: {},
    timeIndex: 0,
    counts: 50,
    open: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    this.setData({ userInfo });
    this.timer();
    this.webSocket();
  },
  webSocket () {
    // 创建Socket 
    wx.connectSocket({
      // 192.168.100.233:8099
      url: 'wss://socket.199.nuomayun.com:8099/pay/5f0d456078d004lfy04/' + this.data.userInfo.mobile,
      header:{
        'content-type': 'application/json',
      },
      timeout:5000,//超时时间，单位为毫秒
      success:(e)=>{//接口调用成功的回调函数
        console.log(e,'创建Websocket 连接成功')
      },
      fail:(e)=>{//接口调用失败的回调函数
        console.log(e,'失败')
      }
    })
    wx.onSocketOpen(res => {
      let set = setInterval(() => {
      if(this.data.open == true){
        wx.sendSocketMessage({
          data:'getPayCode',
          success: (e) => {
            this.setData({ open: false })
          }
        })
      }
      },10)
    })
    wx.onSocketMessage((e) =>{
      let data = JSON.parse(e.data);
      console.log(data)
      if(data.actionName == '付款码'){
        let payCode = JSON.parse(e.data).data.payCode;
        if(payCode){
          this.setData({ refreshStatus: false })
          setTimeout(() => {
            this.setData({ refreshStatus: true })
          }, 1000)
        }else{
          wx.showToast({ icon: 'none',title: '刷新失败' })
        }
        barcode('barcode', payCode, 600, 150);
        qrcode('qrcode', payCode, 462, 462);
      }else{
        wx.showToast({ icon: 'none',title: data.data.title,duration: 5000,icon: 'success', });
        this.setData({ open: true })
      }
      
    })
},
  // 刷新二维码
  refresh () {
    this.setData({ open: true });
  },
  onHide () {
    console.log('sfasfasfafs')
    var that = this;
    clearInterval(that.data.timeIndex);

  },
  onUnload () {
    var that = this;
    clearInterval(that.data.timeIndex);
  },
  //定时器
  timer() {
    var that = this;
    that.data.timeIndex = setInterval(function () {
      var times = --that.data.counts;
      that.setData({
        counts: times
      });
      if (that.data.counts < 1) {
        that.setData({
          counts: 50,
          open: true
        });
      }
    }, 1000);

  },
  //获取当前滑块的index
  bindchange:function(e){
    const that  = this;
    console.log(e.detail.current)
    that.setData({
      currentData: e.detail.current
    })
  },
  // 禁止滑动
  catchTouchMove:function(res){
    return false
  },
  // 点击切换，滑块index赋值
  checkCurrent:function(e){
    const that = this;
    if(e.target.dataset.current == 1){
      wx.showToast({
        icon: 'none',
        title: '功能暂未开放',
      })
      return false;
    }
    if (that.data.currentData === e.target.dataset.current){
        return false;
    }else{
      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  // 预览二维码
  previewImage (e) {
    let as = this
    let canvasId = e.currentTarget.dataset.name;
    wx.canvasToTempFilePath({
      canvasId,
      fileType: 'jpg',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(res);
        wx.previewImage({
          current: tempFilePath, // 当前显示图片的http链接  
          urls: [tempFilePath] // 需要预览的图片http链接列表  
        })
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onUnload () {
    console.log(1);
    wx.closeSocket();
    clearInterval(this.data.timeIndex);
  }
})